<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['emperor'])) {
    $_SESSION['emperor'] = [
        'treasury' => 10000, // 国库资金
        'population' => 1000, // 人口数量
        'army' => 100, // 军队规模
        'palace_level' => 1 // 宫殿等级
    ];
}

// 定义各项操作的消耗和收益
$costs = [
    'recruit_army' => [
        'treasury' => 10,
        'population' => 1
    ],
    'build_palace' => [
        'treasury' => 5000
    ]
];

// 处理征兵操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recruit_army'])) {
    if ($_SESSION['emperor']['treasury'] >= $costs['recruit_army']['treasury'] && $_SESSION['emperor']['population'] >= $costs['recruit_army']['population']) {
        $_SESSION['emperor']['treasury'] -= $costs['recruit_army']['treasury'];
        $_SESSION['emperor']['population'] -= $costs['recruit_army']['population'];
        $_SESSION['emperor']['army'] += 1;
        $message = "成功征兵 1 人，国库减少 {$costs['recruit_army']['treasury']}，人口减少 {$costs['recruit_army']['population']}。";
    } else {
        $message = "资源不足，无法征兵。";
    }
}

// 处理建设宫殿操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['build_palace'])) {
    if ($_SESSION['emperor']['treasury'] >= $costs['build_palace']['treasury']) {
        $_SESSION['emperor']['treasury'] -= $costs['build_palace']['treasury'];
        $_SESSION['emperor']['palace_level']++;
        $message = "成功建设宫殿，宫殿等级提升至 {$_SESSION['emperor']['palace_level']}，国库减少 {$costs['build_palace']['treasury']}。";
    } else {
        $message = "国库资金不足，无法建设宫殿。";
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟中国古代皇帝游戏</title>
    <style>
        body {
            font-family: "SimSun", serif;
            background-color: #f4e9d3;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #6b4423;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        button {
            padding: 10px 20px;
            background-color: #a0764c;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #835d36;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>模拟中国古代皇帝游戏</h1>
    <p>国库资金: <?php echo $_SESSION['emperor']['treasury']; ?></p>
    <p>人口数量: <?php echo $_SESSION['emperor']['population']; ?></p>
    <p>军队规模: <?php echo $_SESSION['emperor']['army']; ?></p>
    <p>宫殿等级: <?php echo $_SESSION['emperor']['palace_level']; ?></p>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '成功')!== false? 'success' : 'error'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="post">
        <button type="submit" name="recruit_army">征兵（国库 - <?php echo $costs['recruit_army']['treasury']; ?>，人口 - <?php echo $costs['recruit_army']['population']; ?>）</button>
    </form>

    <form method="post">
        <button type="submit" name="build_palace">建设宫殿（国库 - <?php echo $costs['build_palace']['treasury']; ?>）</button>
    </form>
</body>

</html>